package com.example.rabbitextensionsdemo.listener;

import com.example.rabbitextensionsdemo.constant.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class DLXListener {
//    @RabbitListener(queues = Constants.NORMAL_QUEUE)
    public void normalListener(Message message, Channel channel) throws Exception{
        Long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            System.out.printf("[normal.queue]队列接收到消息: %s, deliveryTag: %s\n",new String(message.getBody()),deliveryTag);
            System.out.println("处理业务逻辑");
            int num = 3/0;
            // basicAck(deliveryTag, 是否批量确认)
            channel.basicAck(deliveryTag,false);
        }catch (Exception e) {
            // basicNack (deliveryTag, 是否批量确认, 是否打回到队列中)
            channel.basicNack(deliveryTag,true,false);
        }
    }

//    @RabbitListener(queues = Constants.DLX_QUEUE)
    public void dlxListener(Message message, Channel channel) throws Exception{
        Long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("[dlx.queue]死信到消息: %s, deliveryTag: %s\n",new String(message.getBody()),deliveryTag);
    }
    @RabbitListener(queues = Constants.DLX_QUEUE)
    public void delayListener(Message message) {
        Long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("接受到消息 %tc\n", new Date());
        System.out.printf("[dlx.queue]死信到消息: %s, deliveryTag: %s\n",new String(message.getBody()),deliveryTag);
    }
}
